package com.cqut.ktpspring.service.impl;

import com.cqut.ktpspring.mapper.CourseMapper;
import com.cqut.ktpspring.mapper.CourseMemberMapper;
import com.cqut.ktpspring.pojo.Course;
import com.cqut.ktpspring.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CourseServiceimpl implements CourseService {
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private CourseMemberMapper courseMemberMapper;
    @Override
    //新增
    public void addCourse(Course course) {
        courseMapper.insert(course);
    }
    //查询所有课程信息
    @Override
    public List<Course> select() {
        return courseMapper.select();
    }

    //更新课程信息
    @Override
    @Transactional
    public void update(Course course) {
        if (course.getArchiveStatus() != 0) {
            courseMemberMapper.changeAllStatus(course.getId(),course.getArchiveStatus()==1?1:0);
        } else if (course.getDeleted()!=0) {
            courseMemberMapper.changeAllStatus(course.getId(),course.getDeleted()==1?2:0);
        }
        courseMapper.updateById(course);
    }

    //删除课程信息
    @Override
    public void delete(String id) {
        courseMapper.delete(id);
    }

    //课程信息获取
    @Override
    public Course getCourseById(String id) {
        return courseMapper.selectById(id);
    }


}
